//#include <conio.h>
#include <iostream>
#include <unistd.h>     // For sleep
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/time.h> 
#include <netinet/in.h>
#include <stdio.h>
#include <string.h>
#include <ncurses.h>
#include <sys/timeb.h>


#define WIDTH 30
#define HEIGHT 10

const int PORT = 1024;

using namespace std;

int btnTime1 = 0;
int btnTime2 = 0;
int btnTime3 = 0;
int btnTime4 = 0;

const int msDelay = 500;

int getMilliCount(){
    timeb tb;
    ftime(&tb);
    int nCount = tb.millitm + (tb.time & 0xfffff) * 1000;
    return nCount;
}
int getMilliSpan(int nTimeStart){
    int nSpan = getMilliCount() - nTimeStart;
    if(nSpan < 0)
        nSpan += 0x100000 * 1000;
    return nSpan;
}

int main(void)
{
    int startx = (80 - WIDTH) / 2;
    int starty = (24 - HEIGHT) / 2;
    initscr();
    WINDOW *menu_win;
    clear();
    noecho();
    cbreak();
    menu_win = newwin(HEIGHT, WIDTH, 0, 0);
    keypad(menu_win, TRUE);

    int sock;
    struct sockaddr_in addr;

    sock = socket(AF_INET, SOCK_STREAM, 0);
    if(sock < 0)
    {
        perror("socket");
        return(1);
    }

    addr.sin_family = AF_INET;
    addr.sin_port = htons(PORT); // или любой другой порт...
    addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
    if(connect(sock, (struct sockaddr *)&addr, sizeof(addr)) < 0)
    {
        perror("connect");
        return(2);
    }

    int key=0;
    unsigned char* pMessage;
    int currentTime;
    struct timeval clockBegin;
    
    while(key!=27)//for exit press "ESC"
    {
        //if(kbhit())//buffer keyboard not empty?
        {
            //key=getchar();
            key = wgetch(menu_win);
            switch (key)
            {
            case KEY_UP:
                currentTime = getMilliCount();
                printf("  UP: currentTime=%d\n",currentTime);
                if (getMilliSpan(btnTime1) > msDelay)
                {
                    pMessage = new unsigned char[5];
                    pMessage[0] = 0x3;
                    pMessage[1] = 0xDD;
                    pMessage[2] = 0x1;
                    pMessage[3] = 0x0;
                    pMessage[4] = 0x0;
                    send(sock, pMessage, 5, 0);
                    delete[] pMessage;
                    btnTime1 = currentTime;
                }
                break;
            case KEY_LEFT:
                currentTime = getMilliCount();
                printf("LEFT: currentTime=%d\n",currentTime);
                if (getMilliSpan(btnTime2) > msDelay)
                {
                    pMessage = new unsigned char[5];
                    pMessage[0] = 0x05;
                    pMessage[1] = 0xDE;
                    pMessage[2] = 0x00;
                    pMessage[3] = 0x00;
                    pMessage[4] = 0x00;
                    send(sock, pMessage, 5, 0);
                    delete[] pMessage;
                    btnTime2 = currentTime;
                }
                break;
            case KEY_DOWN:
                currentTime = getMilliCount();
                printf("DOWN: currentTime=%d\n",currentTime);
                if (getMilliSpan(btnTime3) > msDelay)
                {
                    pMessage = new unsigned char[5];
                    pMessage[0] = 0x04;
                    pMessage[1] = 0xAA;
                    pMessage[2] = 0x1;
                    pMessage[3] = 0x0;
                    pMessage[4] = 0x0;
                    send(sock, pMessage, 5, 0);
                    delete[] pMessage;
                    btnTime3 = currentTime;
                }
                break;
            case KEY_RIGHT:
                currentTime = getMilliCount();
                printf("RIGHT: currentTime=%d\n",currentTime);
                if (getMilliSpan(btnTime4) > msDelay)
                {
                    pMessage = new unsigned char[5];
                    pMessage[0] = 0x6;
                    pMessage[1] = 0xCC;
                    pMessage[2] = 0x0;
                    pMessage[3] = 0x0;
                    pMessage[4] = 0x0;
                    send(sock, pMessage, 5, 0);
                    delete[] pMessage;
                    btnTime4 = currentTime;
                }
                break;
            }
        }
    }
    
    //recv(sock, buf, sizeof(message), 0);
    
    //printf(buf);
    close(sock);


/*
    char key=0;
    while(key!=27)//for exit press "ESC"
    {
        if(kbhit())//buffer keyboard not empty?
        {
        key=getch();
            switch (key)
            {
            case 72:
                cout<<"Up arrow press"<<endl;
                break;
            case 75:
                cout<<"Left arrow press"<<endl;
                break;
            case 80:
                cout<<"Down arrow press"<<endl;
                break;
            case 77:
                cout<<"Right arrow press"<<endl;
            }
        }
    }
    */
}